//
//  LiveBroadcastController.m
//  斗鸡TV
//
//  Created by admin on 16/3/10.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "LiveBroadcastController.h"
#import "LiveModel.h"
#import "UIScrollView+SVPullToRefresh.h"
#import "LiveBroadCell.h"
#import "AVPlayerViewController.h"
#import "HeadReusableView.h"
#import "ReactiveCocoa.h"
#import "MoreViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


#define Live_url @"http://120.27.183.232/v4/user/qudanSubscribed.json?appName=general_video&appVersion=3.6.6&partner=0100&platform=ios&profile=general_men&sid=cd18452f65ece0686810e3b45c97857f&size=1000&start=0"
@interface LiveBroadcastController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UICollectionView * collectView;
    WaQuModel * sHM;
    NSMutableArray * dataArr;
}
@end

@implementation LiveBroadcastController

- (void)viewDidLoad {
    [super viewDidLoad];
    dataArr = [NSMutableArray new];
    

    UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(WIDTH/2-2, 130);
    layout.minimumInteritemSpacing = 1;
    
    collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64-48) collectionViewLayout:layout];
    collectView.backgroundColor = [UIColor whiteColor];
    collectView.dataSource = self;
    collectView.delegate = self;
    
    [collectView registerClass:[HeadReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    [collectView registerClass:[LiveBroadCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collectView];

    __weak LiveBroadcastController * weakSelf = self;
    [collectView addPullToRefreshWithActionHandler:^{
        
        [weakSelf laodHttpData];
    }];
    [self laodHttpData];
    
    
}




-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeadReusableView * reusableview = [HeadReusableView new];
    

    reusableview.backgroundColor = [UIColor orangeColor];
    
    reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
    WaQuModel * hm = dataArr[indexPath.section];
    reusableview.array = dataArr;
    [reusableview setHeadViewTitle:hm.name index:indexPath];

    
    return reusableview;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return dataArr.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return  CGSizeMake(WIDTH, 30);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    WaQuModel * model = dataArr[section];
    return model.videos.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LiveBroadCell * cell  = [collectView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    WaQuModel * wm = dataArr[indexPath.section];
    SubWaQuModel * sm = wm.videos[indexPath.row];
    [cell setCellData:sm];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AVPlayerViewController * avPlay = [AVPlayerViewController new];
    WaQuModel * model = dataArr[indexPath.section];
     SubWaQuModel * sm = model.videos[indexPath.row];
    avPlay.wuquModel = sm;
    avPlay.mid = model.id;
    [self.navigationController pushViewController:avPlay animated:YES];
}

-(void)laodHttpData
{
//    [self showLoading];
//
//    [self httpGetUrl:Live_url params:nil successBlock:^(NSDictionary *dict) {
//
//        NSArray * arr = [dict arrayForKey:@"hotQudans"];
//        if (arr)
//        {
//            for (NSDictionary * dic in arr) {
//
//                sHM = [[WaQuModel alloc] initWithDictionary:dic error:nil];
//                [dataArr addObject:sHM];
//            }
//
//        }
//
//        [collectView reloadData];
//        [collectView.pullToRefreshView stopAnimating];
//        [self dismissLoading];
//    } errorBlock:^(NSError *error) {
//        [collectView.pullToRefreshView stopAnimating];
//
//
//    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
